package com.slg.android.manyanime.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.LoginActivity;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.Comment;
import com.slg.android.manyanime.bean.User;
import com.slg.android.manyanime.bean.WatchUser;
import com.slg.android.manyanime.utils.DataStore;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by E540 on 2015/3/16.
 */
public class CommentAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<Comment> data;
    private AlertDialog dialog;
    private User loginUser;
    private Animation animation;


    public CommentAdapter(Context context, List<Comment> data) {
        this.context = context;
        this.data = data;
        initDialog();
        loginUser = DataStore.getDataStore().getUser();
        animation = AnimationUtils.loadAnimation(context, R.anim.anim_common_diminish);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommentViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_comment_last, parent, false);
            holder = new CommentViewHolder();
            convertView.setTag(holder);
        }
        holder = (CommentViewHolder) convertView.getTag();
        ViewUtils.inject(holder, convertView);
        Comment comment = data.get(position);
        WatchUser user = comment.getFromUser();
        holder.name.setText(user.getNickName());
        holder.context.setText(comment.getContent());
        holder.time.setText(comment.getCreateTime());
        holder.replyNum.setText(comment.getReplyNum());
        holder.proNum.setText(comment.getGoodNum());
        if ("1".equals(comment.getIsPraise())) {
            holder.proImg.setImageResource(R.drawable.content_pro_hover);
        }
        BitmapUtils bitmapUtils = DataStore.getBitmapUtils();
        bitmapUtils.configDefaultLoadingImage(R.drawable.icon_default);
        bitmapUtils.display(holder.pic, user.getHeadPic(), new BitmapLoadCallBack<ImageView>() {
            //设置圆形头像
            @Override
            public void onLoadCompleted(ImageView container, String uri, Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {
                int width = bitmap.getWidth();
                Bitmap dst = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                Canvas canvas = new Canvas(dst);
                canvas.drawCircle(width / 2, width / 2, width / 2, paint);
                container.setImageBitmap(dst);
            }

            @Override
            public void onLoadFailed(ImageView container, String uri, Drawable drawable) {
                container.setImageDrawable(drawable);
            }
        });
        holder.context.setOnClickListener(this);
        ArrayList<Object> tag = new ArrayList<>();
        tag.add(position);
        tag.add(holder.proAnim);
        tag.add(holder.proNum);
        holder.proImg.setTag(tag);
        holder.proImg.setOnClickListener(this);
        holder.replyImg.setOnClickListener(this);
        holder.pic.setOnClickListener(this);

        return convertView;
    }

    @Override
    public void onClick(View v) {
        int vId = v.getId();
        switch (vId) {
            case R.id.item_comment_pro_icon://点赞
                if (loginUser == null) {
                    dialog.show();
                } else {
                    HttpUtils httpUtils = new HttpUtils();
                    ArrayList<Object> tag = (ArrayList<Object>) v.getTag();
                    final TextView view = (TextView) tag.get(1);
                    Comment c = data.get((Integer) tag.get(0));
                    String isPraise = c.getIsPraise();
                    if (TextUtils.equals("0", isPraise)) {//未赞
                        httpUtils.send(HttpRequest.HttpMethod.GET
                                , "http://manhua.api.61.com/?&pf=1&android_version=16&app_version=512&app_channel=%E7%99%BE%E5%BA%A6&m=Index&a=praiseComment&id=" + c.getId() + "&uid=" + loginUser.getId() + "&flag=1&type=good"
                                , null);
                        view.setText("+1");
                        view.setVisibility(View.VISIBLE);
                        animation.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                view.setVisibility(View.GONE);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        view.startAnimation(animation);
                        c.setIsPraise("1");
                        int goodNum = Integer.parseInt(c.getGoodNum()) + 1;
                        ((TextView) tag.get(2)).setText(String.valueOf(goodNum));
                        ((ImageView) v).setImageResource(R.drawable.content_pro_hover);
                    } else {
                        httpUtils.send(HttpRequest.HttpMethod.GET
                                , "http://manhua.api.61.com/?&pf=1&android_version=16&app_version=512&app_channel=%E7%99%BE%E5%BA%A6&m=Index&a=praiseComment&id=" + c.getId() + "&uid=" + loginUser.getId() + "&flag=2&type=good"
                                , null);
                        view.setText("-1");
                        view.setVisibility(View.VISIBLE);
                        animation.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                view.setVisibility(View.GONE);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        view.startAnimation(animation);
                        c.setIsPraise("0");
                        int goodNum = Integer.parseInt(c.getGoodNum());
                        ((TextView) tag.get(2)).setText(String.valueOf(goodNum));
                        ((ImageView) v).setImageResource(R.drawable.content_pro_default);
                    }
                }
                break;
            case R.id.item_comment_reply_icon://回复
                if (loginUser == null) {
                    dialog.show();
                } else {
                    //TODO 回复界面
                }
                break;
            case R.id.item_comment_pic:
                //TODO 跳转到用户界面
                break;
            case R.id.item_comment_content:
                break;
            case R.id.dialog_negative:
                dialog.dismiss();
                break;
            case R.id.dialog_sure:
                context.startActivity(new Intent(context, LoginActivity.class));
                break;
        }
    }

    private void initDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.item_my_notice_remote_all, null);
        TextView title = (TextView) view.findViewById(R.id.dialog_title);
        title.setText("登录提醒");
        TextView content = (TextView) view.findViewById(R.id.dialog_content);
        content.setText("您尚未登录，请登录");
        Button sure = (Button) view.findViewById(R.id.dialog_sure);
        float scale = context.getResources().getDisplayMetrics().density;
        int pxSize = (int) (12 * scale + 0.5f);
        //sure.setTextSize(20);
        sure.setText("登录");
        sure.setOnClickListener(this);
        Button negative = (Button) view.findViewById(R.id.dialog_negative);
        negative.setOnClickListener(this);
        //negative.setTextSize(20);
        negative.setText("我再看看");
        builder.setView(view);
        dialog = builder.create();
    }

    public static class CommentViewHolder {
        @ViewInject(R.id.item_comment_pic)
        public ImageView pic;
        @ViewInject(R.id.item_comment_username)
        public TextView name;
        @ViewInject(R.id.item_comment_content)
        public TextView context;
        @ViewInject(R.id.item_comment_time)
        public TextView time;
        @ViewInject(R.id.item_comment_reply_num)
        public TextView replyNum;
        @ViewInject(R.id.item_comment_pro_num)
        public TextView proNum;
        @ViewInject(R.id.item_comment_reply_icon)
        public ImageView replyImg;
        @ViewInject(R.id.item_comment_pro_icon)
        public ImageView proImg;
        @ViewInject(R.id.item_comment_pro_anim)
        public TextView proAnim;

    }

}
